package com.example.myhuangye;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class Main2Activity extends AppCompatActivity {
    private WebView webView;
    List<user_kuaidi.lucheng> list = new ArrayList<user_kuaidi.lucheng>();
    @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        final String kuaid[] = {"yuantong","yunda","shunfeng","ems","shentong","tiantian","dadao"};
        final String kuaids[] = {"圆通","韵达","顺丰","EMS","申通","天天","大道"};
        final EditText editText = (EditText) findViewById(R.id.kuaihao);
        final Spinner spinner = (Spinner) findViewById(R.id.kuaiming);
        final ListView lis = (ListView) findViewById(R.id.list_vo);
        findViewById(R.id.kuaicha).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String a = "";
                for (int i = 0;i <kuaids.length;i++){
                        if (spinner.getSelectedItem().toString().trim().equals(kuaids[i])) {
                            a = kuaid[i];
                        }
                }//获取快递的标识以供接口使用


                gethttp("http://www.kuaidi100.com/query?type="+a+"&postid="+editText.getText().toString().trim(),new Handler(){
                    @Override
                    public void handleMessage(Message msg) {
                        super.handleMessage(msg);//解析网络数据  网络请求
//                        List<user_kuaidi.lucheng> vv = new ArrayList<user_kuaidi.lucheng>();
                        Gson g = new Gson();              //用来解析json数据
                        user_kuaidi u = g.fromJson(msg.obj.toString(),user_kuaidi.class);//fromJson  解析
                        user_kuaidi aa = new user_kuaidi();
                        for (int i = 0;i<u.getData().size();i++){
                            user_kuaidi.lucheng ff = aa.new lucheng();
                            //Toast.makeText(Main2Activity.this, ""+u.getData().get(i).getTime(), Toast.LENGTH_SHORT).show();
                            ff.setTime(u.getData().get(i).getTime());
                            ff.setFtime(u.getData().get(i).getFtime());
                            ff.setContext(u.getData().get(i).getContext());
                            ff.setLocation(u.getData().get(i).getLocation());
                            list.add(ff);
                        }
                        //Toast.makeText(Main2Activity.this, ""+list.toString(), Toast.LENGTH_SHORT).show();
                        lis.setAdapter(new adapter());//使用适配器往listview里面添加数据
                    }
                });
            }
        });
        findViewById(R.id.HT222).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    class adapter extends BaseAdapter{
                                            //适配器
        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int i) {
            return null;
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            user_kuaidi.lucheng uu = list.get(i);
            view = getLayoutInflater().inflate(R.layout.kuaidi_item,null);
            TextView time = (TextView) view.findViewById(R.id.time)
                    ,context = (TextView) view.findViewById(R.id.context)
                    ,location = (TextView) view.findViewById(R.id.location);
            time.setText(uu.getTime());
            context.setText(uu.getContext());
            location.setText(uu.getLocation());
            return view;
        }
    }






    public static void gethttp(final String path, final Handler handler){
        //访问网络数据  原生的网络请求
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {

                    String json;
                    URL url=new URL(path);
                    HttpURLConnection conn= (HttpURLConnection) url.openConnection();
                    conn.setRequestMethod("GET");
                    InputStream in=conn.getInputStream();
                    BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(in));
                    json=bufferedReader.readLine();
                    Message message=handler.obtainMessage();
                    message.obj=json;
                    handler.sendMessage(message);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();

    }
    //网络连接
}




